﻿using System;
using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;

namespace UcAsp.Net.NOSQLDAL
{
    public class CategoriesDAL : UcAsp.Net.IDAL.ICategoriesDAL
    {
        public List<Categories> GetCategories()
        {
            return STSData.GetInstance.Select<Model.Categories>();
        }

        public List<Categories> Select(int parentid, int pagesize, int pageindex, out int pagecount)
        {
            List<Categories> list = new List<Categories>();
            var query = GetCategories().Where(o => o.ParentID == parentid);
            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }

        public Categories GetCategoriesByShowPath(string path)
        {
            return GetCategories().FirstOrDefault(o => o.ShowPath == path);
        }

        public List<Categories> SelectAll()
        {
            return GetCategories();
        }

        public Categories GetCategoriesById(int id)
        {
            Categories categories = new Categories();
            categories = GetCategories().FirstOrDefault(o => o.ID == id);
            return categories;
        }

        public Categories Insert(Categories model)
        {
            long key;
            Model.Categories lastrow = STSData.GetInstance.LastRow<Model.Categories>(out key);
            model.ID = Convert.ToInt32(key.ToString()) + 1;
            STSData.GetInstance.Insert<Model.Categories>(model.ID, model);
            return model;
        }
        public List<UcAsp.Net.Model.Categories> GetCategoriesSubject(string categories)
        {
            return new List<Categories>();
        }
        public bool Update(Categories model)
        {
            STSData.GetInstance.Update<Model.Categories>(model.ID, model);
            return true;
        }

        public bool Delete(int id)
        {
            STSData.GetInstance.Delete<Model.Categories>(id);
            return true;
        }

        public List<Categories> SelectList(int parentid)
        {
            int pagecount = 0;
            return Select(parentid, 200, 0, out pagecount);
        }

        public List<Model.CategoriesTree> GetCategoriessTree(int parentid, List<Model.CategoriesTree> list, int i)
        {
            i++;

            foreach (Model.Categories c in new CategoriesDAL().SelectList(parentid))
            {
                CategoriesTree m = new CategoriesTree { CategorieName = c.CategorieName, Depth = i, ID = c.ID, ParentID = c.ParentID };
                list.Add(m);
                GetCategoriessTree(c.ID, list, i);
            }

            return list;
        }

        public List<Model.CategoriesTree> GetCategoriessPath(int id)
        {
            int i = 0;
            List<Model.CategoriesTree> list = new List<CategoriesTree>();
            if (id == 0)
            {
                return list;
            }
            Categories c = new CategoriesDAL().GetCategoriesById(id);
            int parentid = c.ParentID;
            CategoriesTree m = new CategoriesTree { CategorieName = c.CategorieName, Depth = i, ID = c.ID, ParentID = c.ParentID };
            list.Add(m);
            while (parentid != 0)
            {
                i++;
                c = new CategoriesDAL().GetCategoriesById(parentid);
                m = new CategoriesTree { CategorieName = c.CategorieName, Depth = i, ID = c.ID, ParentID = c.ParentID };
                parentid = c.ParentID;
                list.Add(m);
            }
            return list.OrderByDescending(o => o.Depth).ToList();
        }

        public List<Model.Select> GetCategoriessComBox()
        {
            List<Model.CategoriesTree> list = GetCategoriessTree(0, new List<Model.CategoriesTree>(), 0);
            List<Model.Select> CategoryComBox = new List<Model.Select>();
            foreach (Model.CategoriesTree c in list)
            {
                Model.Select box = new Model.Select();
                string _temp = string.Empty;
                box.Name = _temp + c.CategorieName;
                box.Value = c.ID.ToString();
                box.Parent = c.ParentID;
                box.Depth = c.Depth;
                box.ShowPath = c.ShowPath;
                Categories ct = GetCategories().FirstOrDefault(o => o.ParentID == c.ID);
                if (ct != null)
                {
                    box.IsParent = true;
                }
                else
                {
                    box.IsParent = false;
                }
                CategoryComBox.Add(box);
            }
            return CategoryComBox;
        }
    }
}